<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

/* const animationDuration = 6000*/

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '480px'
    },
    barData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    echartsResize(){
      if (this.chart) {
        this.chart.resize()
      }
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          type: 'scroll',
          left: 'center',
          right: '20',
          data: this.barData.data.legend
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.barData.data.xAxisData
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: this.barData.data
        /* title: {
          text: this.barData.title,
          x: 'center',
          padding: 16,
          textStyle: {
            fontWeight: 'normal',
            color: '#fff'
          }
        },
        color: ['#32dadd', '#c8b2f4', '#63c2ff', '#ffcb8c'],
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线， 可选为：'line' | 'shadow'
          }
        },
        legend: {
          left: 'center',
          bottom: '10'
        },
        xAxis: {
          type: 'category',
          data: this.barData.name,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#ffffff'
            }
          }
        },
        yAxis: {
          type: 'value',
          name: '',
          axisLabel: {
            textStyle: {
              color: '#ffffff'
            }
          },
          splitLine: {
            show: false
          }
        },
        series: [
          {
            type: 'bar',
            data: this.barData.data,
            barWidth: '40%',
            itemStyle: {
              normal: {
                // 每个柱子的颜色即为colorList数组里的每一项，如果柱子数目多于colorList的长度，则柱子颜色循环使用该数组
                // color: (params) => {
                //   var colorD = ['#32dadd', '#c8b2f4', '#63c2ff', '#ffcb8c']
                //   if(this.barData.color){
                //     colorD = this.barData.color
                //   }
                //   var colorList = colorD; //每根柱子的颜色
                //   return colorList[params.dataIndex];
                // }

                // 柱子的颜色渐变
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1,
                  [
                    { offset: 0, color: '#0cffcc' },
                    // {offset: 0.5, color: '#188df0'},
                    { offset: 1, color: '#1d97ca' }
                  ]
                ),
                barBorderRadius: [50, 50, 0, 0]
              },
              emphasis: {
                barBorderRadius: [50, 50, 0, 0]
              }
            }
          }
        ]*/
      })
    }
  }
}
</script>

